package document.controller;


import document.dao.DocumentDao;
import document.entity.Document;
import org.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class DocumentListServlet extends HttpServlet {

    private final DocumentDao documentDao = new DocumentDao();
    private static final long serialVersionUID = 1;

    public DocumentListServlet() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        doPost(request, response);
    }

    /**
     * 项目成员和项目经理查看自己所在项目的文档列表
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        System.out.println("进入了DocumentListServlet");
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");
        //这里看前端是否传入参数，若没有则查询全部用户，否则进行条件查询
        int page =Integer.parseInt(request.getParameter("page"));
        int limit = Integer.parseInt(request.getParameter("limit"));
        int userId = Integer.parseInt(request.getParameter("userId"));
        String documentName = request.getParameter("documentName");
        List<Document> listDocument = documentDao.documentList(documentName, page, limit, userId);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("count",listDocument.size());
        jsonObject.put("data", listDocument);
        //这里状态码需要返回0，因为是layui数据表格模板要求
        jsonObject.put("code","0");
        System.out.println(jsonObject.toString());
        response.getWriter().write(jsonObject.toString());
    }
}
